package com.naiterui.ehp.bs.b2c.order.constants;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import lombok.Getter;

@Getter
public enum OrderStatusEnum {

    /**
     * 待付款
     */
    PENDING(1, "待付款", 1, "待付款"),
    /**
     * 处方流转成功
     */
    TRANSFER_SUCCESS(2, "处方流转成功", 2, "处方流转成功"),
    /**
     * 订单已发货
     */
    SHIPPING(3, "订单已发货", 3, "订单已发货"),
    /**
     * 订单已签收
     */
    RECEIVED(4, "订单已签收", 5, "已结束"),
    /**
     * 已取消
     */
    CANCELLED(5, "已取消", 5, "已结束"),
    /**
     * 处方流转失败
     */
    TRANSFER_FAIL(6, "处方流转失败", 2, "处方流转失败"),

    /**
     * 待审核
     */
    WAIT_AUDIT(7, "待审核", 2, "待发货"),
    /**
     * 订单审核完成
     */
    AUDIT_COMPLETE(8, "订单审核完成", 2, "待发货"),
    /**
     * 库房分配完成
     */
    ASSIGNED_COMPLETE(9, "库房分配完成", 2, "待发货"),
    /**
     * 药店已确认订单
     */
    WAREHOUSE_CONFIRM(10, "药店已确认订单", 2, "待发货"),

    ;

    /**
     * 根据订单状态值获取订单状态枚举
     */
    public static OrderStatusEnum getEnumByStatus(Integer status) {
        OrderStatusEnum[] enums = OrderStatusEnum.values();
        for (OrderStatusEnum e : enums) {
            if (e.getStatus().equals(status)) {
                return e;
            }
        }
        throw new IllegalArgumentException(String.format("订单状态不正确 %d", status));
    }

    /**
     * 通过后台状态获取前台状态
     */
    public static OrderStatusEnum getFrontDeskStatus(int orderStatus) {
        OrderStatusEnum[] values = OrderStatusEnum.values();
        for (OrderStatusEnum value : values) {
            if (value.getStatus() == orderStatus) {
                return value;
            }
        }
        throw new IllegalArgumentException(String.format("传入的订单状态不正确 %d", orderStatus));
    }

    /**
     * 通过前段订单状态获取后台订单状态
     */
    public static Set<Integer> getStatusByFrontDeskStatus(int frontDeskStatus) {
        OrderStatusEnum[] values = OrderStatusEnum.values();
        Set<Integer> orderStatus = Arrays.stream(values)
                                         .filter(value -> value.getFrontDeskStatus() == frontDeskStatus)
                                         .map(OrderStatusEnum::getStatus)
                                         .collect(Collectors.toSet());
        if (CollectionUtil.isEmpty(orderStatus)) {
            throw new IllegalArgumentException(String.format("传入的订单状态不正确 %d", frontDeskStatus));
        }
        return orderStatus;
    }

    /**
     * 业务订单状态
     */
    private Integer status;
    /**
     * 业务订单状态描述
     */
    private String statusDescribe;
    /**
     * 前台显示状态
     */
    private Integer frontDeskStatus;
    /**
     * 前台显示状态描述
     */
    private String frontDeskStatusDescribe;

    OrderStatusEnum(Integer status, String statusDescribe, Integer frontDeskStatus, String frontDeskStatusDescribe) {
        this.status = status;
        this.statusDescribe = statusDescribe;
        this.frontDeskStatus = frontDeskStatus;
        this.frontDeskStatusDescribe = frontDeskStatusDescribe;
    }

}
